<?php

require_once "MainController.php";

/**
 * Contrôleur par défaut
 *
 * @package Controller
 */
class IndexController extends MainController
{
    /**
     * Action par défaut
     *
     * @return null
     */
    public function indexAction()
    {
        $this->view->lastMoviesAdded = array();
        $this->view->bestRatedMovies = array();
        $this->view->nbMovies = array();
        $this->view->friends = array();


        //$this->_logger->debug(print_r($this->_connection, true));

//         try {
            // .js index
            //$this->view->headScript()->appendFile($this->view->baseUrl() . "/js/addFriend.js");

            //$userId = $this->_connection->id;

            $movieMapper = new App_Model_MovieMapper();
            $userMapper = Adapter::getUserMapper();

            $movieList = $movieMapper->getLastMoviesAdded(10);
            $this->view->lastMoviesAdded = $movieList;
            //$this->_logger->debug(print_r($movieList, true));

            // FIXME : simplifier la requete
            $movieList = $movieMapper->getBestRatedMovies(10);
            $this->view->bestRatedMovies = $movieList;

            //$nbMovies = $movieMapper->getNbMoviesByUserID($userId);
//             $this->_logger->debug(print_r($nbMovies, true));
//             $this->_logger->debug("nbMovies<$nbMovies>");
            //$this->view->nbMovies = $nbMovies;

            //$friends = $userMapper->getFriendsByUserID($userId);
            //$this->view->friends = $friends;

            //         $requests = $userMapper->getRequestsByUserID($userId);
            //         $this->view->requests = $requests;
//         }
//         catch (Exception $e) {
//             $this->_logger->err($e);
//         }
    }

    /**
     * Vide le cache des traductions et redirige vers la page  des traductions
     */
    public function emptycacheAction()
    {
        // vider le cache
        Zend_Translate::clearCache();

        // redirection
        $redirector = $this->_helper->getHelper('Redirector');
        $redirector->setGotoSimple('index');
    }

    /**
     * enregistre une demande d'ajout d'ami
     */
//     public function addfriendAction()
//     {
//         $params = $this->_request->getParams();

//         if (!isset($params['f'])) {
//             $this->_response->clearBody();
//             $this->_response->clearHeaders();
//             $this->_response->setHttpResponseCode(406);
//             return;
//         }

//         $friend = $params['f'];
//         $friend = $this->view->escape($friend);
//         // TODO : verification de la chaine $friend

//         /* User */
//         $userId = $this->_connection->id;
//         $userMapper = Adapter::getUserMapper();

//         // on verifie s'ils sont pas deja amis
// //         $userMapper->getFriendsByUserID($userId);
//         // TODO

//         // recuperation de l'idFriend
//         $idFriend = -1;
//         $queryParams1 = array('dbName' => 'email', 'value' => $friend);
//         $queryParams2 = array('dbName' => 'login', 'value' => $friend);
//         $friendResults = $userMapper->findUsersByParams(array($queryParams1));
//         if (count($friendResults) == 1)
//             $idFriend = $friendResults[0]->idUser;
//         else {
//             $friendResults = $userMapper->findUsersByParams(array($queryParams2));
//             if (count($friendResults) == 1)
//                 $idFriend = $friendResults[0]->idUser;
//         }

//         if ($idFriend == -1) {
//             $return = array('success' => false);
//         }
//         else {
//             // ajout dans la table rt_user_request
//             $userMapper->addRequest($userId, $idFriend);

//             $return = array('success' => true);
//         }
//         $this->_helper->json($return);
//     }

}

